% To read the 1 min ae index file from wdc kyoto

function[data,fday] = read_wdc_ae_index(fname)


fid = fopen(fname,'rt');

if fid == -1,

    disply('Error in opening a file');

    return;

end;

    

A= fscanf(fid,'%c',[401,inf]);

L = A(19,:) == 'E';
A = A(:,L);

if any(A(:,end) == ''),

    A(:,end) = [];

end;



fclose(fid);

y=str2num(A(13:14,:)');



if y > 10 & y ~= 0,%somewhere in the 20th century

    y = y + 1900;

else % in 21st centuey

    y = y + 2000;

end;



m=str2num(A(15:16,:)');

d=str2num(A(17:18,:)');

h=str2num(A(20:21,:)');



fday = datenum(y,m,d,h,0,0);

cmp = upper(A(19,:)');


data = zeros([60,length(fday)]);

st = 35;

en = 40;

for i = 1:60,

    data(i,:) = str2num(A(st:en,:)');

    st = en + 1;

    en = st + 5;

end;



L = data == 999999 | abs(data) > 80000;

data(L) = NaN;

fday_min = repmat(fday,[1,60]) + repmat(datenum(0,0,0,0,0:59,30),[length(fday),1]);
data = reshape(data,[1,60*length(fday)]);
fday = reshape(fday_min',[1,60*length(fday_min)]);


